package com.example.demoplus.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.demoplus.R;
import com.example.demoplus.ShopDetailActivity;
import com.example.demoplus.bean.ShopBean;
import java.util.ArrayList;
import java.util.List;

public class ShopAdapter extends BaseAdapter {
//    上下文对象
    private Context context;
//    商品列表的数据
    private List<ShopBean> data = new ArrayList<>();

    public ShopAdapter(Context context) {
        this.context = context;
    }

    public List<ShopBean> getData() {
        return data;
    }

    public void setData(List<ShopBean> data) {
        //先清空数据
        this.data.clear();
        //再添加数据，避免数据被改变导致无法刷新数据
        this.data.addAll(data);
//        进行数据的刷新
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        //做数据的绑定
        //判断view是否为空
        if (convertView == null){
            //数据加载到哪个文件
            convertView = LayoutInflater.from(context).inflate(R.layout.shop_item, null);
//            Log.d("twm","convertView"+convertView);
            //对viewHolder做绑定
            viewHolder = new ViewHolder();
            //从布局里找到ID
            viewHolder.tvOfferPrice = convertView.findViewById(R.id.tv_offer_price);
            viewHolder.tvShopName = convertView.findViewById(R.id.tv_shop_name);
            viewHolder.tvSaleNum = convertView.findViewById(R.id.tv_sale_num);
            viewHolder.tvWelfare = convertView.findViewById(R.id.tv_welfare);
            viewHolder.tvTime = convertView.findViewById(R.id.tv_time);
            viewHolder.ivShopPic = convertView.findViewById(R.id.iv_shop_pic);
            //将viewHolder放到布局里
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //根据位置获取对应的值
        ShopBean shopBean = data.get(position);
        //图片是从网络上获取，所以需要下载下来。用Glide控件获取
        //.with()初始化对象  load()URL地址来  into()放入到哪个地方 error()如果没有图片显示一个错误图片
//        为每个控件设置值
        Glide.with(context).load(shopBean.getShopPic()).error(R.mipmap.ic_launcher).into(viewHolder.ivShopPic);
        viewHolder.tvOfferPrice.setText("起送￥"+shopBean.getOfferPrice()+"配送￥"+shopBean.getDistributionCost());
        viewHolder.tvTime.setText(shopBean.getTime());
        viewHolder.tvShopName.setText(shopBean.getShopName());
        viewHolder.tvSaleNum.setText("月售额"+shopBean.getSaleNum());
        viewHolder.tvWelfare.setText(shopBean.getWelfare());
//        为这个页面做一个事件监听，方便跳转到详情页，并且将数据带到详情页中
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                测试监听是否有效
//                Toast.makeText(conte xt,shopBean.getShopName(), Toast.LENGTH_SHORT).show();
//                跳转到ShopDetailActivity去
                Intent intent = new Intent(context, ShopDetailActivity.class);
//                将数据带过去  ShopBean是序列化的
                intent.putExtra("shopBean",shopBean);
                context.startActivity(intent);
            }
        });

        return convertView;
    }
    class ViewHolder{
        public TextView tvShopName, tvSaleNum, tvOfferPrice, tvWelfare, tvTime;
        public ImageView ivShopPic;
    }
}
